﻿using Adnc.Application.Shared.Services;
using Adnc.Application.Shared.WeChat;
using Adnc.Infra.IRepositories;
using System;
using System.Threading.Tasks;
using WYT.Usr.Application.Contracts.Dtos.GZH;
using WYT.Usr.Application.Contracts.Services;
using WYT.Usr.Repository.Entities;

namespace WYT.Usr.Application.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class GZHAppService : AbstractAppService, IGZHAppService
    {

        private readonly IEfRepository<User> _userRepository;

        private readonly WeChatService _weChatService;

        public GZHAppService(IEfRepository<User> userRepository
            , WeChatService weChatService)
        {
            _userRepository = userRepository;
            _weChatService = weChatService;
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<AppSrvResult> PushMessage(MessagePushDto input)
        {
            var user = await _userRepository.FindAsync(input.UserId);
            if (user != null && !string.IsNullOrEmpty(user.GongZongHaoOpenID))
            {
                var result = await _weChatService.ExecuteCgibinMessageTemplateSendAsync(user.GongZongHaoOpenID,input.TemplateId, input.Pagepath,input.Data);
                if (!result.IsSuccessful())
                    return Problem(null, result.ErrorMessage);
            }
            return AppSrvResult();
        }
    }
}
